package com.chhuang.main;

import java.sql.Timestamp;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.chhuang.bean.OperateBean;
import com.chhuang.bean.QueryTermBean;
import com.chhuang.common.DateUtil;
import com.chhuang.common.ThreadPoolShutdownUtil;
import com.chhuang.sync.AddSyncThread;
import com.chhuang.sync.FullQueryAddSyncThread;
import com.chhuang.sync.FullSyncThread;

/**
 * 同步任务启动类
 * @author HuangChen
 *
 */
public class SyncMain {
	private static final Log LOG = LogFactory.getLog(SyncMain.class);
	
	private static ExecutorService pool = null;
	
	public void execute(){
		LOG.info("Data synchronization begin");
		long start = System.currentTimeMillis();
		
		pool = Executors.newFixedThreadPool(10);
		
		//全量同步表
		pool.execute(new FullSyncThread("test","m_test",null));
		
		//全量比对增量同步  操作参数类中的true表示数据表有物理删除需要同步
		pool.execute(new FullQueryAddSyncThread("test","m_test",null,new OperateBean(2,true,"DZBZDSZL_DM")));
		
		//增量同步表  操作参数类中的false表示数据表没有物理删除不需要同步删除
		String modifyTimeColumn = "modifyTime";
		QueryTermBean initQueryTermBean = new QueryTermBean("modifyTime>=?",new Timestamp(DateUtil.string2Date("2016-01-01").getTime()));
		OperateBean operateBean = new OperateBean(2,false,"id");
		pool.execute(new AddSyncThread("test1","m_test1",modifyTimeColumn,initQueryTermBean,operateBean));
		
		ThreadPoolShutdownUtil.shutdownPool(pool, 20, TimeUnit.HOURS);
		
		long end = System.currentTimeMillis();
		LOG.info("Data synchronization completed. spent "+DateUtil.ms2String(end-start, DateUtil.HH_MM_SS_SSS));
	}
	
	/**
	 * 运行execute方法
	 * @param args
	 */
	public static void main(String[] args) {
		new SyncMain().execute();
	}
}
